package org.uni_hi.lsep.gui;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.uni_hi.lsep.AppManager;
import org.uni_hi.lsep.gui.test.AssignmentTabbedPane;
import org.uni_hi.lsep.gui.test.DragNDropTestPanel;
import org.uni_hi.lsep.gui.test.QuizAnswerPanel;
import org.uni_hi.lsep.gui.test.ResultPanel;
import org.uni_hi.lsep.gui.test.sim.SimulationGraphicPanel;
import org.uni_hi.lsep.gui.test.sim.SimulationsSettingsPanel;
import org.uni_hi.lsep.util.FileManager;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TestPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel headingLabel = null;
	private QuizAnswerPanel quizPanel = null;
	
	private ResultPanel resultPanel=null;

	private DragNDropTestPanel dragndropPanel = null;

	private SimulationGraphicPanel simVisualPanel = null;
	private SimulationsSettingsPanel simsettingsPanel = null;

	private AssignmentTabbedPane assignmentTabbedPane = null;
	
	public TestPanel(){
		init();
	}
	
	private void init(){
		int width=980;
		headingLabel = new JLabel();
		headingLabel.setText("Kapitel / Unterkapitel");
		headingLabel.setBounds(10, 0, 300, 20);
		
		this.setLayout(null);
		this.add(headingLabel);
		this.add(getAssignmentTabbedPane());
		getAssignmentTabbedPane().setBounds(0, headingLabel.getHeight()+1,width-2,150);
		
		getSimVisualPanel().getPanel().setLocation((width-getSimVisualPanel().getPanel().getWidth())/2, getAssignmentTabbedPane().getHeight()+getAssignmentTabbedPane().getY()+5);
		getSimsettingsPanel().setBounds(0, getSimVisualPanel().getPanel().getHeight()+getSimVisualPanel().getPanel().getY()+1, width, 250);

		Dimension size=new Dimension(getSimVisualPanel().getPanel().getWidth(),getSimsettingsPanel().getHeight()+getSimsettingsPanel().getY()+1); //
		Rectangle ws_size=new Rectangle(0,(getAssignmentTabbedPane().getHeight()+getAssignmentTabbedPane().getY()+5),width,(size.height-getAssignmentTabbedPane().getHeight()-30)); //workspace size

		getQuizPanel().setBounds(ws_size);
		getDragndropPanel().setBounds(ws_size);
		getResultPanel().setBounds(ws_size);

		this.setLocation(0,0);
		this.setSize(size);
		this.setPreferredSize(size);
	}

	/**
	 * This method initializes simVisualPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public SimulationGraphicPanel getSimVisualPanel() {
		if (simVisualPanel == null) {
			simVisualPanel = new SimulationGraphicPanel();
		}
		return simVisualPanel;
	}

	/**
	 * This method initializes simsettingsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public SimulationsSettingsPanel getSimsettingsPanel() {
		if (simsettingsPanel == null) {
			simsettingsPanel = new SimulationsSettingsPanel(getAssignmentTabbedPane());			
		}
		return simsettingsPanel;
	}
	
	/**
	 * This method initializes assignmentTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	public AssignmentTabbedPane getAssignmentTabbedPane() {
		if (assignmentTabbedPane == null) {
			assignmentTabbedPane = new AssignmentTabbedPane();
		}
		return assignmentTabbedPane;
	}
	
	/**
	 * This method initializes answersPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private QuizAnswerPanel getQuizPanel() {
		if (quizPanel == null) {
			quizPanel=new QuizAnswerPanel();
		}
		return quizPanel;
	}

	private ResultPanel getResultPanel(){
		if(resultPanel==null){
			resultPanel=new ResultPanel();
		}
		return resultPanel;
	}

	/**
	 * This method initializes dragndropPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private DragNDropTestPanel getDragndropPanel() {
		if (dragndropPanel == null) {
			dragndropPanel=new DragNDropTestPanel();
		}
		return dragndropPanel;
	}
	
	public void setTestData(NodeList nl){
		NodeList tnl=null;
		Node n=nl.item(0);
		String type=n.getAttributes().getNamedItem("type").getNodeValue();
		String name=n.getAttributes().getNamedItem(FileManager.NAME_ATT).getNodeValue();
		
		if("sim".equals(type)){//all actions necessary for the simulation
			addSimPanel(false);
		}else if("free_sim".equals(type)){
			addSimPanel(true);
			AppManager.getMainFrame().getMainMenuBar().setLoadSaveEnabled(true);
			//XXX ? remove the task pane ("Aufgabe") if free simulation is selected?
		}else if("drag_drop".equals(type)){
			addDragDropPanel();
		}else if("quiz".equals(type)){
			addQuizPanel();
		}else if("result".equals(type)){
			addResultPanel(null);
		}
		
		headingLabel.setText(name);
		tnl=n.getChildNodes();
//		n.
		for(int i=0;i<tnl.getLength();i++){
			n=tnl.item(i);
			if(FileManager.DESCRIPTION_ATT.equals(n.getNodeName())){
				getAssignmentTabbedPane().setAssignmentText(n.getTextContent());
			}else if("packet".equals(n.getNodeName())){
				getAssignmentTabbedPane().addPacket(n);
			}else if("answer".equals(n.getNodeName())){
				getQuizPanel().addAnswer(n);
			}else if("dropfield".equals(n.getNodeName())){
				getDragndropPanel().addDropFields(n);
			}else if("dragfield".equals(n.getNodeName())){
				getDragndropPanel().addDragFields(n);
			}
		}
	}

	public void addSimPanel(boolean editable) {
		this.remove(getDragndropPanel());
		this.remove(getQuizPanel());
		this.remove(getResultPanel());
		this.add(getSimVisualPanel().getPanel());
		this.add(getSimsettingsPanel());
		getAssignmentTabbedPane().addPacketPanel();
		getAssignmentTabbedPane().setPacketsEditable(editable);
		getAssignmentTabbedPane().removePackets();
		getAssignmentTabbedPane().setSelectedIndex(0);
	}

	private void addDragDropPanel() {
		this.remove(getSimVisualPanel().getPanel());
		this.remove(getSimsettingsPanel());
		this.remove(getQuizPanel());
		this.remove(getResultPanel());
		getAssignmentTabbedPane().removePacketPanel();
		this.add(getDragndropPanel());
		getAssignmentTabbedPane().removePacketPanel();
		getDragndropPanel().removeFields();
	}

	private void addQuizPanel() {
		this.remove(getSimVisualPanel().getPanel());
		this.remove(getSimsettingsPanel());
		this.remove(getDragndropPanel());
		this.remove(getResultPanel());
		getAssignmentTabbedPane().removePacketPanel();
		this.add(getQuizPanel());
		getQuizPanel().removeAnswers();
	}

	public void addResultPanel(int[] results) {
		this.remove(getSimVisualPanel().getPanel());
		this.remove(getSimsettingsPanel());
		this.remove(getQuizPanel());
		this.remove(getDragndropPanel());
		getAssignmentTabbedPane().removePacketPanel();
		this.add(getResultPanel());
		getResultPanel().setAnswerResults(results);
		repaint();
	}
}
